﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Platformer
{
    public class Smoke
    {
        // Needed variables
        PlatformerGame game;
        Vector2 position;
        Texture2D texture;

        // Properties
        Point size = new Point(77, 100);
        Rectangle rect;
        Point currentFrame = new Point(0, 0);
        float scale = 0.8f;
        int frameTimer, frameRate = 4;
        int frameNumber = 17;
        int loopPhaseTimer, maxTimeLoop = 6;

        public bool Active
        {
            get { return active; }
            set { active = value; }
        }
        bool active = false;

        public int MaxIterations
        {
            get { return maxIterations; }
            set { maxIterations = value; }
        }
        int maxIterations = 0;

        public int CurrentIterations
        {
            get { return currentIterations; }
            set { currentIterations = value; }
        }
        int currentIterations = 0;

        public Smoke(PlatformerGame game, Vector2 position)
        {
            this.game = game;
            this.position = position;
            texture = game.Content.Load<Texture2D>("Character/Sprites/smoke1");
            rect = new Rectangle(0, 0, size.X, size.Y);
            maxIterations = frameRate * frameNumber;
        }

        public void Update()
        {
            if (Keyboard.GetState().IsKeyDown(Character.keyJetpack) && !active)
            {
                active = true;
                currentFrame = new Point(0, 0);
            }
            if (active)
            {
                if (game.Player.CurrentDirection == Character.Direction.Right)
                    position = game.Player.Position + new Vector2(-20, 45);
                else if (game.Player.CurrentDirection == Character.Direction.Left)
                    position = game.Player.Position + new Vector2(5, 45);
                frameTimer++;

                if (frameTimer >= frameRate)
                {
                    frameTimer = 0;
                    // Loop phase
                    if (currentFrame.Y == 0)
                    {
                        loopPhaseTimer++;
                        if (Keyboard.GetState().IsKeyDown(Character.keyJetpack))
                            loopPhaseTimer = 0;
                        if (currentFrame.X < 3)
                            currentFrame.X++;
                        // Last frame of the loop
                        else if (currentFrame.X == 3)
                        {
                            if (loopPhaseTimer > maxTimeLoop)
                            {
                                currentFrame.Y++;
                                currentFrame.X = 0;
                            }
                            else
                                currentFrame.X = 0;
                        }
                    }
                    // End phase
                    else if (currentFrame.Y == 1)
                    {
                        if (Keyboard.GetState().IsKeyDown(Character.keyJetpack))
                        {
                            currentFrame.Y = 0;
                            currentFrame.X = 0;
                            loopPhaseTimer = 0;
                        }
                        if (currentFrame.X < 11)
                            currentFrame.X++;
                        else if (currentFrame.X == 11)
                        {
                            active = false;
                            currentFrame = new Point(0, 0);
                        }
                    }
                    rect = new Rectangle(currentFrame.X * size.X, currentFrame.Y * size.Y, size.X, size.Y);
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            if(active)
                spriteBatch.Draw(texture, position, rect, Color.White, 0f, Vector2.Zero, scale, SpriteEffects.None, 0f);

            spriteBatch.DrawString(PlatformerGame.debugFont, "Jetpack.X :" + currentFrame.X, new Vector2(400, 0), Color.Red);
            spriteBatch.DrawString(PlatformerGame.debugFont, "Jetpack.Y :" + currentFrame.Y, new Vector2(400, 25), Color.Red);

            spriteBatch.DrawString(PlatformerGame.debugFont, "Time in loop :" + loopPhaseTimer, new Vector2(400, 50), Color.Red);
        }

        /* Custom */

    }
}
